package book.crypto;

import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;

import javax.crypto.KeyAgreement;
import javax.crypto.SecretKey;

public class KeyAgreementTest {

	public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException, IllegalStateException {
		
		KeyPairGenerator gen = KeyPairGenerator.getInstance("DH");
		KeyPair pair1 = gen.generateKeyPair();
		KeyPair pair2 = gen.generateKeyPair();
		KeyAgreement keyAgree = KeyAgreement.getInstance(gen.getAlgorithm());
		keyAgree.init(pair2.getPrivate());
		keyAgree.doPhase(pair1.getPublic(), true);
		SecretKey key = keyAgree.generateSecret("DES");
	}
}
